package com.scholarship.modules.admin.controller;

import com.github.pagehelper.PageInfo;
import com.scholarship.common.dto.ResultListDto;
import com.scholarship.common.entity.RetCode;
import com.scholarship.common.entity.RetResponse;
import com.scholarship.common.entity.RetResult;
import com.scholarship.common.utils.JsonUtil;
import com.scholarship.modules.admin.dto.*;
import com.scholarship.modules.admin.entity.RewardFeedback;
import com.scholarship.modules.admin.exce.EmptyValueException;
import com.scholarship.modules.admin.service.FeedbackListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Z.J.H
 * @create 2021/5/9 14:37
 */
@Slf4j
@Api(tags = {"申请审核管理控制层"})
@RestController
public class FeedbackController {

    @Autowired
    private FeedbackListService feedbackListService;

    /**
     * 获取审核列表查询条件下拉框信息
     * @return
     */
    @ApiOperation("获取审核列表查询条件下拉框信息")
    @GetMapping({"/admin/feedback/query","/backend/feedback/query"})
    public RetResult getStudentQuery() {
        try {
            Map<String, List<String>> map = feedbackListService.getFeedbackQuery();
            return RetResponse.makeRsp(RetCode.SUCCESS,"获取查询信息成功",map);
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeRsp(RetCode.INTERNAL_SERVER_ERROR,"未知错误");
        }
    }

    /**
     * 获取所有审核信息
     * @return
     */
    @ApiOperation("获取所有审核信息")
    @GetMapping("/admin/feedback/all")
    public RetResult listAllFeedbackInfo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        try {
            ResultListDto<FeedbackListDto> result = feedbackListService.listAllFeedback(pageNum,pageSize);
            return RetResponse.makeOKRsp(result);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeErrRsp("列表信息获取失败");
        }
    }

    /**
     * 根据查询条件查询审核信息
     * @param feedbackQueryDto
     * @return
     */
    @ApiOperation("根据查询条件查询审核信息")
    @GetMapping("/admin/feedback/list")
    public RetResult listFeedbackInfo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                      FeedbackQueryDto feedbackQueryDto) {
        try {
            ResultListDto<FeedbackListDto> result = feedbackListService.listFeedbackByQuery(pageNum,pageSize,feedbackQueryDto);
            return RetResponse.makeOKRsp(result);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeErrRsp("列表信息获取失败");
        }
    }

    /**
     * 根据搜索查询审核信息
     * @param searchStr
     * @return
     */
    @ApiOperation("根据搜索查询审核信息")
    @GetMapping("/admin/feedback/search")
    public RetResult listFeedbackInfo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                      @RequestParam("searchStr") String searchStr) {
        try {
            ResultListDto<FeedbackListDto> result = feedbackListService.listFeedbackBySearch(pageNum,pageSize,searchStr);
            return RetResponse.makeOKRsp(result);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeErrRsp("列表信息获取失败");
        }
    }

    /**
     * 批量删除审核信息
     * @param userIdStr 例如：101,102,103(用','分隔)
     * @return
     */
    @ApiOperation("批量删除审核信息")
    @PostMapping("/admin/feedback/delete")
    public RetResult batchDeleteFeedbackInfo(String userIdStr) throws Exception {
        if(userIdStr == null || userIdStr.equals("")) {
            return RetResponse.makeRsp(RetCode.EMPTY_CONTENT,"内容为空或无选中信息");
        }
        //用','分隔userId
        String[] userIdList = userIdStr.split(",");
        for(String str : userIdList) {
            System.out.println(str);
        }
        try {
            feedbackListService.batchDeleteFeedbacks(userIdList);
            return RetResponse.makeRsp(RetCode.SUCCESS,"删除成功");
        } catch (Exception e) {
            return RetResponse.makeRsp(RetCode.FAIL,"删除失败");
        }
    }

    /**
     * 获取奖学金类型列表
     * @return
     * @throws Exception
     */
    @ApiOperation("获取奖学金类型列表")
    @GetMapping("/admin/feedback/type")
    public RetResult listAllRewardType() throws Exception {
        try {
            return RetResponse.makeOKRsp(feedbackListService.listAllRewardType());
        }catch (Exception e) {
            e.printStackTrace();
            return RetResponse.makeRsp(RetCode.FAIL,"未知错误");
        }
    }

    /**
     * 获根据奖学金类型id取奖项列表
     * @param typeId
     * @return
     * @throws Exception
     */
    @ApiOperation("获取奖项列表")
    @GetMapping("/admin/feedback/detail")
    public RetResult listRewardTypeDetail(@RequestParam("typeId") Integer typeId) throws Exception {
        try {
            return RetResponse.makeOKRsp(feedbackListService.listRewardTypeDetailByTypeId(typeId));
        }catch (Exception e) {
            e.printStackTrace();
            return RetResponse.makeRsp(RetCode.FAIL,"未知错误");
        }
    }

    /**
     * 通过奖项id查询字段列表
     * @param tableId
     * @return
     * @throws Exception
     */
    @ApiOperation("获取字段列表")
    @GetMapping("/admin/feedback/field")
    public RetResult listTableInfo(@RequestParam("tableId") Integer tableId) throws Exception {
        try {
            return RetResponse.makeOKRsp(feedbackListService.listTableInfoByTableId(tableId));
        }catch (Exception e) {
            e.printStackTrace();
            return RetResponse.makeRsp(RetCode.FAIL,"未知错误");
        }
    }

    /**
     * 通过奖项id查询奖项下拉框列表
     * @param tableId
     * @return
     * @throws Exception
     */
    @ApiOperation("获取下拉框列表")
    @GetMapping({"/admin/feedback/box","/backend/feedback/box"})
    //@RequiresRoles(value = "admin")
    public RetResult listDropDownBox(@RequestParam("tableId") Integer tableId) throws Exception {
        try {
            return RetResponse.makeOKRsp(feedbackListService.listDropDownBoxByTableIdAndFieldId(tableId,4));
        }catch (Exception e) {
            e.printStackTrace();
            return RetResponse.makeRsp(RetCode.FAIL,"未知错误");
        }
    }

    /**
     * 显示奖学金申请层级表（奖学金类型+奖项+字段+下拉框+已填写信息）
     * @param userId
     * @return
     * @throws Exception
     */
    @ApiOperation("显示奖学金申请层级表（奖学金类型+奖项+字段+下拉框+已填写信息）")
    @GetMapping("/admin/feedback/info")
    public RetResult listRewardListAndInformation(@RequestParam("userId") String userId) throws Exception {
        try {
            return RetResponse.makeOKRsp(feedbackListService.listRewardListAndInformation(userId));
        }catch (Exception e) {
            e.printStackTrace();
            return RetResponse.makeRsp(RetCode.FAIL,"未知错误");
        }
    }

    /**
     * 保存指定奖项信息
     * @return
     * @throws Exception
     */
    @ApiOperation("保存指定奖项信息")
    @PostMapping("/admin/feedback/save")
    public RetResult SaveSelectedReward(@RequestBody SaveInfoDto saveInfoDto) throws Exception {
        try {
            feedbackListService.saveSelectedRewardTypeDetail(saveInfoDto.getUserId(),saveInfoDto.getRewardTypeDetailDto());
            return RetResponse.makeRsp(RetCode.SUCCESS,"保存成功");
        } catch (EmptyValueException e) {
            return RetResponse.makeRsp(RetCode.EMPTY_CONTENT,e.getLocalizedMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return RetResponse.makeRsp(RetCode.FAIL,e.getLocalizedMessage());
        }
    }

    /**
     * 获取申请信息详情
     * @param userId
     * @return
     */
    @ApiOperation("获取总申请信息详情")
    @GetMapping("/admin/feedback/application")
    public RetResult getApplication(@RequestParam("userId") String userId) {
        try {
            RewardFeedback result = feedbackListService.getApplication(userId);
            return RetResponse.makeOKRsp(result);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeErrRsp("信息获取失败");
        }
    }

    /**
     * TODO:提交申请
     * @return
     * @throws Exception
     */
    @ApiOperation("提交申请")
    @PostMapping("/admin/feedback/submit")
    public RetResult submitApplication(@RequestParam("userId") String userId) throws Exception {
        try {
            feedbackListService.submitApplication(userId);
            return RetResponse.makeRsp(RetCode.SUCCESS,"申请成功");
        }catch (Exception e) {
            e.printStackTrace();
            return RetResponse.makeRsp(RetCode.FAIL,e.getLocalizedMessage());
        }
    }
}
